package com.yoomiito.app.utils;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.View;

import com.yoomiito.app.R;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class SpannableUtils {

    public static SpannableString getIconAndText(String str, Drawable drawable) {
        SpannableString spanText = new SpannableString(" " + str);
        drawable.setBounds(0, 0, DensityUtil.dp2px(24), DensityUtil.dp2px(14));
        spanText.setSpan(new ImageSpan(drawable), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString otherTextColor(String str, int color, int start, int end) {
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new ForegroundColorSpan(ResUtil.getColor(color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getIconAndOtherTextColor(String str, int ivId,int ivStart,int ivEnd,int ivWidth,int ivHeight,int color, int colorStart, int colorEnd) {
        SpannableString spanText = new SpannableString(" " + str);
        spanText.setSpan(new ForegroundColorSpan(ResUtil.getColor(color)), colorStart, colorEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        Drawable drawable = ResUtil.getDrawable(ivId);
        drawable.setBounds(0, 0, DensityUtil.dp2px(ivWidth), DensityUtil.dp2px(ivHeight));
        spanText.setSpan(new ImageSpan(drawable),ivStart,ivEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getSizeAndPriceText(String str,int color,int colorStart,int colorEnd,int sizeStart,int sizeEnd){
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new RelativeSizeSpan(0.65f),sizeStart,sizeEnd , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new ForegroundColorSpan(ResUtil.getColor(color)),colorStart,colorEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getSmallMoneyFlag(String str){
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new RelativeSizeSpan(0.5f),0,1 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getSmallMoneyFlag(String str,float size){
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new RelativeSizeSpan(size),0,1 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getTime(String str,Drawable drawable){
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new StyleSpan(Typeface.BOLD),0,2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new RelativeSizeSpan(0.7f),2, str.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new ImageSpan(drawable),1,2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getDiscountPrice(String s){
        SpannableString spanText = new SpannableString(s);
        if (TextUtils.isEmpty(s)||s.length()==1){
            return spanText;
        }
        spanText.setSpan(new RelativeSizeSpan(0.5f),0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (s.contains(".")){
            spanText.setSpan(new RelativeSizeSpan(0.5f),s.length()-2,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spanText;
    }

    public static SpannableString handlePriceDecimal(String s){
        SpannableString spanText = new SpannableString(s);
        spanText.setSpan(new RelativeSizeSpan(0.75f),s.length()-2,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    public static SpannableString getDiscountPrice(String s,int textColor){
        SpannableString spanText = new SpannableString(s);
        spanText.setSpan(new RelativeSizeSpan(0.5f),0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new RelativeSizeSpan(0.5f),s.length()-2,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new ForegroundColorSpan(ResUtil.getColor(textColor)),s.length()-2,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }
    /**
     * 部分文字中划线
     *
     * @param str
     * @param startPosition
     * @return
     */
    public static Spannable getSpannable(String str, int startPosition) {
        SpannableString spannableString = new SpannableString(str);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        spannableString.setSpan(strikethroughSpan, startPosition, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 去掉小数点后的0
     *
     * @param price
     * @return
     */
    public static String handlePrice(String price) {
        if (TextUtils.isEmpty(price)
                || "0".equals(price)
                || "0.0".equals(price)
                || "0.00".equals(price)) {
            return "";
        }

        if (price.endsWith(".00")) {
            return price.substring(0, price.length() - 3);
        }
        if (price.endsWith(".0")) {
            return price.substring(0, price.length() - 2);
        }
        if (price.contains(".") && price.endsWith("0")) {
            return price.substring(0, price.length() - 1);
        }
        return price;
    }

    /**
     * 保留一位小数
     * 超过10000 显示 如1w+
     *
     * @param num
     * @return
     */
    public static String saleNum(String num) {
        double n = Double.parseDouble(num);
        if (n >= 10000) {
            double d = n / 10000;
//            String d1 = new DecimalFormat("0.0").format(d);
            String d1 = String.valueOf(d);
            if (d1.length()- d1.indexOf(".")-1>=2){
                d1 = d1.substring(0, d1.indexOf(".") + 2);
            }
            if (d1.endsWith(".0")) {
                return d1.substring(0, d1.length() - 2) + "w+";
            }
            return d1 + "w+";
        }
        if (num.endsWith(".0")) {
            return num.substring(0, num.length() - 2);
        }
        if (num.endsWith(".00")) {
            return num.substring(0, num.length() - 3);
        }
        return num;
    }
    public static double multiplicationDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.multiply(p2).doubleValue();
    }


    /**
     * 保留两位小数
     * 超过10000 显示 如1w+
     *
     * @param num
     * @return
     */
    public static String saleNum2(String num) {
        double n = Double.parseDouble(num);
        if (n >= 10000) {
            double d = n / 10000;
//            String d1 = new DecimalFormat("0.0").format(d);
            String d1 = String.valueOf(d);
            if (d1.length()- d1.indexOf(".")-1>=3){
                d1 = d1.substring(0, d1.indexOf(".") + 3);
            }
            if (d1.endsWith(".0")) {
                return d1.substring(0, d1.length() - 2) + "w";
            }
            return d1 + "w";
        }
        if (num.endsWith(".0")) {
            return num.substring(0, num.length() - 2);
        }
        if (num.endsWith(".00")) {
            return num.substring(0, num.length() - 3);
        }
        return num;
    }

    public static String saleNum(double num) {
        if (num >= 10000) {
            double d = num / 10000;
//            String d1 = new DecimalFormat("0.0").format(d);
            String d1 = String.valueOf(d);
            if (d1.length()- d1.indexOf(".")-1>=2){
                d1 = d1.substring(0, d1.indexOf(".") + 2);
            }
            if (d1.endsWith(".0")) {
                return d1.substring(0, d1.length() - 2) + "w+";
            }
            return d1 + "w+";
        }
        return String.valueOf((long) num);
    }


    public static boolean isEmptyOr0(String str) {
        if (TextUtils.isEmpty(str)
                || "0".equals(str)
                || "0.0".equals(str)
                || "0.00".equals(str)) {
            return true;
        }
        return false;
    }
}
